﻿using DDE_Server.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DDE_Server.Object
{
    public class PjmInstDispRate
    {
        private DateTime lastUpdate;
        private List<double> rateTable = null;
		private List<double> rateTableSorted = null;
        private bool sorted = false;

        public DateTime LastUpdate {
            set {
                lastUpdate = value;
            }
            get{
                return lastUpdate;
            }
        }
        public PjmInstDispRate()
        {
            rateTable = new List<double>();
        }
        public void updateRateTable(double p)
        {
            rateTable.Add(p);
			sorted = false;
        }
        public List<Double> RateTable
        {
            get
            {
                return rateTable;
            }
        }
        public void Empty()
        {
            rateTable.Clear();
        }

        public void SortRate()
        {
			if (!sorted) {
				sorted = true;
				rateTableSorted = new List<double>(rateTable);
				rateTableSorted.Sort();
			}
        }
        private double Top1()
        {
			//rateTableSorted = new List<double>(rateTable);
			return rateTable.Max();
        }
        private double Top2()
        {
            //if (!sorted) SortRate();
			SortRate();
			if (rateTableSorted.Count > 1)
            {

                return (rateTableSorted[rateTableSorted.Count - 1] + rateTableSorted[rateTableSorted.Count - 2]) / 2.0;
            }
            else
            {
                return 0;
            }
        }
        private double Top3()
        {
            //if (!sorted) SortRate();
			SortRate();
			if (rateTableSorted.Count > 2)
            {

				return (rateTableSorted[rateTableSorted.Count-1] + rateTableSorted[rateTableSorted.Count-2] + rateTableSorted[rateTableSorted.Count-3]) / 3.0;
            }
            else
            {
                return 0;
            }
        }
        private double Top4()
        {
            //if (!sorted) SortRate();
			SortRate();
			if (rateTableSorted.Count > 3)
            {

                return (rateTableSorted[rateTableSorted.Count - 1] + rateTableSorted[rateTableSorted.Count - 2] + rateTableSorted[rateTableSorted.Count - 3] + rateTableSorted[rateTableSorted.Count - 3]) / 4.0;
            }
            else
            {
                return 0;
            }
        }
        private double Avg()
        {
            double sum = 0;
			rateTableSorted = new List<double>(rateTable);
			for (int i = 0; i < rateTableSorted.Count; i++)
            {
				sum += rateTableSorted[i];
            }
			return sum / rateTableSorted.Count;
        }
        public double GetDistpatch(Distpatch dis) 
        {
            switch (dis)
            {
                case Distpatch.TOP1:
                    return Top1();
                case Distpatch.TOP2:
                    return Top2();
                case Distpatch.TOP3:
                    return Top3();
                case Distpatch.TOP4:
                    return Top4();
                case Distpatch.AVG:
                    return Avg();
            }
            return 0;
        }
        public void UpdateListBox() { }
    }
}
